package com.greateamd.timetableasy.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

@SuppressWarnings("serial")
@Entity
@Table(name="study_periods")
public class StudyPeriod extends LightEntity implements PersistableEntity,HasEventsEntity,Serializable
{

	@Id
	@GeneratedValue
	private Integer id;
	private String name;
	private String type;

	@Column(name="enabled",nullable=false)
	private boolean enabled = true;

	@OneToMany(mappedBy="studyPeriod", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
	private Set<Course> courses;
	
	@ManyToOne
	@JoinColumn(name="id_cursus", referencedColumnName="id")
	private Cursus cursus;
	
	@ManyToMany(fetch = FetchType.LAZY,targetEntity=Event.class,cascade=CascadeType.ALL)
    @JoinTable(name="study_periods_events",
		        joinColumns = @JoinColumn(columnDefinition="study_period"),
		        inverseJoinColumns=@JoinColumn(name="event_id"))
	private Set<Event> events;
	
	public StudyPeriod()
	{}

	public StudyPeriod(String name,String type,Cursus cursus) 
	{
		this.name = name;
		this.type = type;
		this.cursus = cursus;
	}

	public Integer getId() {
		return id;
	}

	public String getName() {
		return name;
	}


	public boolean isEnabled() {
		return enabled;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public Cursus getCursus() {
		return cursus;
	}

	public void setCursus(Cursus cursus) {
		this.cursus = cursus;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Set<Course> getCourses() {
		return courses;
	}

	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}
	
	public Set<Event> getEvents() {
		return events;
	}

	private void setEvents(Set<Event> events) {
		this.events = events;
	}

	public void addEvent(Event event) {
		if (events == null)
			events = new HashSet<Event>();
		this.events.add(event);
	}
	
	public void addCourse(Course course) {
		if (courses == null)
			courses = new HashSet<Course>();
		this.courses.add(course);
	}
	
	@Override
	public String toString() {
		return getName();
	}
}
